﻿using System.Collections.Generic;
using System.Linq;
using ML.Entity;
using ML.IBusiness;
using ML.IRepository;

namespace ML.Business
{
    /// <summary>
    /// 收款业务实现类
    /// </summary>
    public class ReceivablesBusiness : IReceivablesBusiness
    {
        private readonly IReceivablesRepository receivablesRepository;
        public ReceivablesBusiness(IReceivablesRepository _receivablesRepository)
        {
            receivablesRepository = _receivablesRepository;
        }

        public BaseResult<Pager<ReceivablesEntity>> List(ReceivablesSearch receivablesSearch)
        {
            Pager<ReceivablesEntity> data = receivablesRepository.List(receivablesSearch);
            return new BaseResult<Pager<ReceivablesEntity>>(200, data);
        }

        public BaseResult<IEnumerable<DropDownList>> GetReceivablesByMerchantId(string merchantId)
        {
            var data = receivablesRepository.GetReceivablesByMerchantId(merchantId);

            //处理成固定的格式返回
            var result = data.Select(n => new DropDownList()
            {
                value = n.Id,
                name = n.Method + " " + n.OpeningName + " " + n.OpeningBank
            });
            return new BaseResult<IEnumerable<DropDownList>>(200, result);
        }

        public BaseResult<bool> Add(ReceivablesEntity entity)
        {
            //业务逻辑：首先将该商户下所有的默认状态设置为未默认，然后将该数据设置为默认
            bool isTrue = receivablesRepository.Add(entity);
            if (isTrue)
            {
                return new BaseResult<bool>(200, true);
            }
            return new BaseResult<bool>(201);
        }

        public BaseResult<bool> Update(ReceivablesEntity entity)
        {
            var isTrue = receivablesRepository.Update(entity);
            if (isTrue)
            {
                return new BaseResult<bool>(200, true);
            }
            return new BaseResult<bool>(201, false);
        }

        public BaseResult<bool> SetDefaultMark(string Id, int DefaultMark)
        {
            //业务：如果传递设置默认，需要将其原来的所有数据设置为非默认，否则直接操作
            var merchanctId = UserHelper.GetCurrentUser().MerchantId;
            var isTrue = false;
            if (DefaultMark == (int)DefaultMarkStatus.defaultmark_true)
            {
                isTrue = receivablesRepository.SetDefaultMarkTrue(Id, DefaultMark, merchanctId);
            }
            else
            {
                isTrue = receivablesRepository.SetDefaultMarkFalse(Id, DefaultMark);

            }
            if (isTrue)
            {
                return new BaseResult<bool>(200, true);
            }
            return new BaseResult<bool>(201, false);
        }

        public BaseResult<bool> Delete(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new BaseResult<bool>(1001, false);
            }

            var isTrue = receivablesRepository.Delete(id);
            if (isTrue)
            {
                return new BaseResult<bool>(200, true);
            }
            return new BaseResult<bool>(201, false);
        }
    }
}